import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { Session } from '/@/utils/storage'
import { useRoutesList } from '/@/stores/routesList'
import { useKeepALiveNames } from '/@/stores/keepAliveNames'
import { staticRoutes, notFoundAndNoPower } from '/@/router/route';
import { NextLoading } from '../utils/loading'
import { initBackEndControlRoutes } from './backEnd'

export const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [...staticRoutes, ...notFoundAndNoPower]
})

/**
 * 路由多级嵌套数组处理成一维数组
 * @param arr 传入路由菜单数据数组
 * @returns 返回处理后的一维路由菜单数组
 */
export function formatFlatteningRoutes(arr: any) {
	if (arr.length <= 0) return false;
	for (let i = 0; i < arr.length; i++) {
		if (arr[i].children) {
			arr = arr.slice(0, i + 1).concat(arr[i].children, arr.slice(i + 1));
		}
	}
	return arr;
}

/**
 * 一维数组处理成多级嵌套数组（只保留二级：也就是二级以上全部处理成只有二级，keep-alive 支持二级缓存）
 * @description isKeepAlive 处理 `name` 值，进行缓存。顶级关闭，全部不缓存
 * @link 参考：https://v3.cn.vuejs.org/api/built-in-components.html#keep-alive
 * @param arr 处理后的一维路由菜单数组
 * @returns 返回将一维数组重新处理成 `定义动态路由（dynamicRoutes）` 的格式
 */
export function formatTwoStageRoutes(arr: any) {
	if (arr.length <= 0) return false;
	const newArr: any = [];
	const cacheList: Array<string> = [];
	arr.forEach((v: any) => {
		if (v.path === '/') {
			newArr.push({ component: v.component, name: v.name, path: v.path, redirect: v.redirect, meta: v.meta, children: [] });
		} else {
			if (v.path.indexOf('/:') > -1) {
				v.meta['isDynamic'] = true;
				v.meta['isDynamicPath'] = v.path;
			}
			newArr[0].children.push({ ...v });
			// 存 name 值，keep-alive 中 include 使用，实现路由的缓存
			// 路径：/@/compontents/layout/routerView/parent.vue
			if (newArr[0].meta.isKeepAlive && v.meta.isKeepAlive) {
				cacheList.push(v.name);
				const stores = useKeepALiveNames();
				stores.setCacheKeepAlive(cacheList);
			}
		}
	});
	return newArr;
}

// 路由加载前
router.beforeEach(async (to, from, next) => {
    NProgress.configure({ showSpinner: false })
    if (to.meta.title) NProgress.start()
    const token = Session.get('token')
    if (to.path === '/login' && !token) {
        next()
        NProgress.done()
    } else {
        if (!token) {
            next(
                `/login?redirect=${to.path}&params=${JSON.stringify(to.query ? to.query : to.params)}`
            )
            Session.clear()
            NProgress.done()
        } else if (token && to.path === '/login') {
            next('/')
            NProgress.done()
        } else {
            const { routesList } = useRoutesList();
            if (routesList.length === 0) {
                await initBackEndControlRoutes()
                next({ path: to.path, query: to.query })
            } else {
                next()
            }
        }
    }
})

// 路由加载后
router.afterEach(() => {
	NProgress.done();
	NextLoading.done();
});

export default router
